﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Framework;
using ESRI.ArcGIS.Controls;
using PlanningGIS.Util.Log;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.Util;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.Product
{
    public partial class FrmRegionQuery : Office2007Form
    {
        public FrmRegionQuery()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        private IApplication m_Application;

        /// <summary>
        /// 绘制工具构建对象
        /// </summary>
        private ToolCreator _ToolCreator = null;

        /// <summary>
        /// 地图关联对象
        /// </summary>
        private IHookHelper m_HookHelp = null;
        /// <summary>
        /// 
        /// </summary>
        public IApplication Application
        {
            set { m_Application = value; }
        }
        
        /// <summary>
        /// 选择的空间位置
        /// </summary>
        public IGeometry SelectedGeometry { get; set; }

        /// <summary>
        /// 是否确定选择
        /// </summary>
        public bool IsOK { get; set; }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.IsOK = false;
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.IsOK = true;
            SelectedGeometry = regionTree.SelectedGeometry;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void FrmRegionQuery_Load(object sender, EventArgs e)
        {
            m_HookHelp = new HookHelper();
            m_HookHelp.Hook = (m_Application as IFramework).Object;

            List<ValueName> btns = new List<ValueName>()
            {
                new ValueName(){Value="ToolDrawRectangle",Name="绘制矩形"},
                new ValueName(){Value="ToolDrawPolygon",Name="绘制多边形"}
            };
            _ToolCreator = new ToolCreator(btnDrawExtent, m_HookHelp.Hook);
            _ToolCreator.HideForm = true;
            _ToolCreator.Create(btns, true);
            _ToolCreator.DrawFinished += OnDrawFinished;

            regionTree.Hook = m_HookHelp.Hook;

            IsOK = false;
        }
        /// <summary>
        /// 绘制完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnDrawFinished(object sender, DrawArgs e)
        {
            regionTree.SelectRegion(e.Geometry, e.Shift);
        }

        private void FrmRegionQuery_FormClosed(object sender, FormClosedEventArgs e)
        {
            (m_Application as IFramework).Tool = null;
            if (_ToolCreator != null) _ToolCreator.Clear();
            regionTree.ClearNodeCheck(regionTree.Nodes);
            regionTree.Clear();
        }
    }
}
